package com.angel.web.app;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.base.JsonEntity;
import com.angel.core.util.DateUtils;
import com.angel.core.util.JsonUtil;
import com.angel.health.BuzCustomerLog;
import com.angel.health.BuzVideo;
import com.angel.provider.health.BuzCustomerLogProvider;
import com.angel.provider.health.BuzVideoProvider;

/**
 * @author 作者姓名 cheng
 * @version 创建时间：2016年10月26日 下午5:43:13 类说明
 */
@Controller
@RequestMapping("app/video")
public class AppVideoController {

	@Autowired
	private BuzVideoProvider bvProvider;
	@Autowired
	private BuzCustomerLogProvider bclProvider;

	/**
	 * 查看用户通话时长
	 * 
	 * @param bv
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "index", produces = "application/json; charset=utf-8")
	public String index(BuzVideo bv) {
		if (bv.getCustomerId() == null) {
			return JsonUtil.toJson(new JsonEntity("-1", "参数不正确"));
		}
		List<Map<String, Object>> list = bvProvider.findMinutes(bv);
		return JsonUtil.toJson(new JsonEntity("200", list.size() > 0 ? list.get(0) : null));
	}

	/**
	 * 通话使用
	 * 
	 * @param bv
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "addVideo", produces = "application/json; charset=utf-8")
	public String addVideo(BuzVideo bv) {
		if(bv.getCustomerId() == null){
			return JsonUtil.toJson(new JsonEntity("-1", "参数错误！"));
		}
		int insert = bvProvider.insert(bv);
		if (insert > 0) {
			// 新增预约日志
			BuzCustomerLog bcl = new BuzCustomerLog();
			if(bv.getVideoMinutes() > 0){
				bcl.setContent("您购买增值服务成功！");
			}else{
				bcl.setContent("您于"+DateUtils.getNowTimeForStr()+"使用增值服务！");
			}
			
			bcl.setType(4);
			bcl.setCustomerId(bv.getCustomerId());
			bclProvider.insert(bcl);
			List<Map<String, Object>> list = bvProvider.findMinutes(bv);
			return JsonUtil.toJson(new JsonEntity("200", list.size() > 0 ? list.get(0) : null));
		} else {
			return JsonUtil.toJson(new JsonEntity("-1", "失败"));
		}
	}
}
